#pragma once

class Helpers
{
    public:

        template<class T>
        static void zeroes(T* ptr)
        {
            memset(ptr, 0, sizeof(T));
        }

        template<class T>
        static void safe_delete(T** ptr)
        {
            delete (*ptr);
            *ptr = nullptr;
        }

        static CString IntToStr(int k)
        {
            TCHAR buf[100];
            memset(buf, 0, sizeof(TCHAR) * 100);
            _itot(k, buf, 10);
            return CString(buf);
        }

        static CString StringFormat(LPCTSTR a_str, ...)
        {
            va_list argptr;
            va_start(argptr, a_str);

            CString str;
            str.FormatV(a_str, argptr);

            return str;
        }

        class StopWatch
        {
            private:

                LARGE_INTEGER m_start;
                LARGE_INTEGER m_stop;
                LARGE_INTEGER m_frequency;
                bool m_started;

            public:

                StopWatch (bool a_auto_start = true)
                {
                    m_start.QuadPart = 0;
                    m_stop.QuadPart = 0;
                    m_started = false;

                    QueryPerformanceFrequency(&m_frequency);

                    if (a_auto_start)
                        StartTimer();
                }

                bool IsStarted()
                {
                    return m_started;
                }

                void StartTimer()
                {
                    if (m_started)
                        return;

                    m_started = true;

                    QueryPerformanceCounter(&m_start);
                }

                void StopTimer()
                {
                    QueryPerformanceCounter(&m_stop);

                    if (!m_started)
                        AfxThrowNotSupportedException();

                    m_started = false;
                }

                int GetElapsedTime()
                {
                    if (m_started)
                        QueryPerformanceCounter(&m_stop);

                    return (int)((m_stop.QuadPart - m_start.QuadPart) * 1000 / m_frequency.QuadPart);
                }
        };
};